package net.melon.sysres.inst.struts2;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import net.esj.basic.core.business.aware.op.OperatorAware;
import net.esj.basic.core.view.support.vojo.BaseVojo;
import net.esj.basic.core.view.support.vojo.TransObject;
import net.esj.basic.exception.MelonException;
import net.esj.basic.exception.MelonRuntimeException;
import net.esj.basic.plugins.view.easyui.utils.EasyUITreeConverter;
import net.esj.basic.plugins.view.easyui.utils.EasyUITreeEntity;
import net.esj.basic.pojo.TreeStructure;
import net.esj.basic.pojo.op.Operator;
import net.esj.basic.utils.Validators;
import net.esj.basic.utils.writer.WriterCode;
import net.melon.sysres.manager.ResourceManager;
import net.melon.sysres.pojo.Resource;

public class ResourceAction extends AbstractSysresAction<Resource, Resource> implements OperatorAware{

	private ResourceManager resourceManager;
	
	private String pId;
	
	private Resource parent;
	
	private Operator operator;
	
	@Override
	public void datagrid() {
		if(!Validators.isEmpty(id)){
			parent = new Resource();
			parent.setId(id);
		}
		List<Resource> resources = resourceManager.loadResources(parent);
		Collection<EasyUITreeEntity> tree = new ResourceEasyUITreeConverter().convert(resources);
		writeJson(tree);
	}
	
	@Override
	public String form() {
		if(!Validators.isEmpty(pId)){
			parent = defaultService.get(pId, Resource.class);
		}
		return super.form();
	}

	public void load() throws MelonRuntimeException {
		if (Validators.isEmpty(id)) {
			return;
		}
		entity = defaultService.get(id, entityClass,voClass);
		ResourceVo vo = (ResourceVo) defaultService.translate(entity, ResourceVo.class);
		writeJson(vo,WriterCode.FORM);
	}
	
	@Override
	public void doAdd() throws MelonException {
		resourceManager.saveResource(entity,true,operator);
	}

	@Override
	public void doUpdate() {
		resourceManager.updateResource(entity,operator);
	}
	

	@Override
	public void doDelete(){
		for (String str : deletes.split(";")) {
			Resource res = defaultService.get(str, Resource.class);
			defaultService.delete(res);
		}
	}

	public Resource getEntity() {
		return entity;
	}

	public void setEntity(Resource entity) {
		this.entity = entity;
	}

	@Inject
	public void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager;
	}
	
	
	public Resource getParent() {
		return parent;
	}


	public String getPId() {
		return pId;
	}



	public void setPId(String pId) {
		this.pId = pId;
	}


	protected static class ResourceEasyUITreeConverter extends EasyUITreeConverter{

		
		
		@SuppressWarnings("unchecked")
		@Override
		protected boolean beforeConvert(Collection treeList) {
			return true;
		}

		@Override
		protected void setExtraValue(EasyUITreeEntity entity,
				TreeStructure treeTarget) {
			super.setExtraValue(entity, treeTarget);
			entity.getAttributes().put("keyType", ((Resource) treeTarget).getKeyType());
			entity.getAttributes().put("keyCode", ((Resource) treeTarget).getKeyCode());
			entity.getAttributes().put("keyValue", ((Resource) treeTarget).getKeyValue());
			entity.getAttributes().put("comments", ((Resource) treeTarget).getComments());
			entity.getAttributes().put("cascading", ((Resource) treeTarget).isCascading());
			entity.getAttributes().put("ordinal", ((Resource) treeTarget).getOrdinal());
			entity.getAttributes().put("hasLeaf", ((Resource) treeTarget).isHasLeaf());
			if(((Resource) treeTarget).isHasLeaf()){
				entity.setState(EasyUITreeEntity.STATE_CLOSE);
			}else{
				entity.setState(EasyUITreeEntity.STATE_OPEN);
			}
		}

	}

	public static class ResourceVo implements TransObject,BaseVojo<String>{

		private String id;
		private String name;
		private String keyType;
		private int keyCode;
		private String keyValue;
		private String comments;
		private int parentCode;
		private Date gmtCreate;
		private Date gmtModify;
		private String creater;
		private String modifier;
		private boolean cascading;
		private int ordinal;
		private Resource parent;
		private int priority;
		private boolean hasLeaf;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getKeyType() {
			return keyType;
		}
		public void setKeyType(String keyType) {
			this.keyType = keyType;
		}
		public int getKeyCode() {
			return keyCode;
		}
		public void setKeyCode(int keyCode) {
			this.keyCode = keyCode;
		}
		public String getKeyValue() {
			return keyValue;
		}
		public void setKeyValue(String keyValue) {
			this.keyValue = keyValue;
		}
		public String getComments() {
			return comments;
		}
		public void setComments(String comments) {
			this.comments = comments;
		}
		public int getParentCode() {
			return parentCode;
		}
		public void setParentCode(int parentCode) {
			this.parentCode = parentCode;
		}
		public Date getGmtCreate() {
			return gmtCreate;
		}
		public void setGmtCreate(Date gmtCreate) {
			this.gmtCreate = gmtCreate;
		}
		public Date getGmtModify() {
			return gmtModify;
		}
		public void setGmtModify(Date gmtModify) {
			this.gmtModify = gmtModify;
		}
		public String getCreater() {
			return creater;
		}
		public void setCreater(String creater) {
			this.creater = creater;
		}
		public String getModifier() {
			return modifier;
		}
		public void setModifier(String modifier) {
			this.modifier = modifier;
		}
		public boolean isCascading() {
			return cascading;
		}
		public void setCascading(boolean cascading) {
			this.cascading = cascading;
		}
		public int getOrdinal() {
			return ordinal;
		}
		public void setOrdinal(int ordinal) {
			this.ordinal = ordinal;
		}
		public Resource getParent() {
			return parent;
		}
		public void setParent(Resource parent) {
			this.parent = parent;
		}
		public int getPriority() {
			return priority;
		}
		public void setPriority(int priority) {
			this.priority = priority;
		}
		public boolean isHasLeaf() {
			return hasLeaf;
		}
		public void setHasLeaf(boolean hasLeaf) {
			this.hasLeaf = hasLeaf;
		}
	}

	public void setParent(Resource parent) {
		this.parent = parent;
	}

	public Operator getOperator() {
		return operator;
	}

	public void setOperator(Operator operator) {
		this.operator = operator;
	}

}
